package com.leyou.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

/**
 * @author 虎哥
 */
@Configuration
public class RateLimitConfig {
    /**
     * 定义一个KeyResolver,方法名就是bean的name
     *
     */
    @Bean
    public KeyResolver ipKeyResolver() {
//        return new KeyResolver() {
//            @Override
//            public Mono<String> resolve(ServerWebExchange exchange) {
//                // 获取请求者的IP地址作为令牌桶的key返回
//                return Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
//            }
//        };
        // JDK8 的Lambda写法：
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
    }
}